package com.zzptc.sky.baidudemo.fragments;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.sky.baidudemo.R;
import com.zzptc.sky.baidudemo.activities.OneKeySOSActivity;
import com.zzptc.sky.baidudemo.activities.SOSForSelectContactsActivity;
import com.zzptc.sky.baidudemo.adapter.ContactsAdapter;
import com.zzptc.sky.baidudemo.base.BaseFragmentsForSOS;
import com.zzptc.sky.baidudemo.utils.MyApplication;
import com.zzptc.sky.baidudemo.utils.MyUntils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/27.
 */

public class SOSSecondFragment extends BaseFragmentsForSOS implements View.OnClickListener {

    private ImageView iv_add_contacts;
    private EditText et_add;
    private Button btn_yes;
    private static RecyclerView rv_contacts;
    private static List<String> contactList = new ArrayList<String>();
    private Button btn_ok;

    @Override
    public int getFragmentXml() {
        return R.layout.fragment_sos_second;
    }

    @Override
    public void initView() {

        iv_add_contacts = (ImageView) getThisView().findViewById(R.id.iv_add_contacts);
        et_add = (EditText) getThisView().findViewById(R.id.et_add);
        btn_yes = (Button) getThisView().findViewById(R.id.btn_yes);
        rv_contacts = (RecyclerView) getThisView().findViewById(R.id.rv_contacts);
        btn_ok = (Button) getThisView().findViewById(R.id.btn_ok);

        iv_add_contacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyApplication.getContext(), SOSForSelectContactsActivity.class));
            }
        });

        et_add.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                    iv_add_contacts.setVisibility(View.GONE);
                    btn_yes.setVisibility(View.VISIBLE);

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().equals("")){
                    iv_add_contacts.setVisibility(View.VISIBLE);
                    btn_yes.setVisibility(View.GONE);
                }
            }
        });

        btn_yes.setOnClickListener(this);
        btn_ok.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_yes:

//                iv_add_contacts.setVisibility(View.VISIBLE);
//                btn_yes.setVisibility(View.GONE);

                String number = et_add.getText().toString().trim();

                if (number.isEmpty() && number.equals("")){
                    Toast.makeText(MyApplication.getContext(), "请输入手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!MyUntils.isPhoneNumber(number)){
                    Toast.makeText(MyApplication.getContext(), "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (contactList.size()>2){
                    Toast.makeText(MyApplication.getContext(), "最多添加3个号码", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    contactList.add(number);
                }

                et_add.setText("");
                Toast.makeText(MyApplication.getContext(), "添加成功", Toast.LENGTH_SHORT).show();

                RecyclerView.LayoutManager ll = new LinearLayoutManager(MyApplication.getContext());
                rv_contacts.setLayoutManager(ll);

                ContactsAdapter contactsAdapter = new ContactsAdapter(contactList);
                rv_contacts.setAdapter(contactsAdapter);

                break;
            case R.id.btn_ok:
                if (MyApplication.getContactsList().size() == 0){

                }else {
                    if (getActivity() instanceof OneKeySOSActivity){
                        ((OneKeySOSActivity) getActivity()).toThreeFragment();
                    }
                }
                break;
        }
    }

    public static void delList(int i){
        if (contactList == null && contactList.isEmpty()){
            Toast.makeText(MyApplication.getContext(), "kong", Toast.LENGTH_SHORT).show();
            return;
        }
        contactList.remove(i);
        Toast.makeText(MyApplication.getContext(), contactList.size()+"", Toast.LENGTH_SHORT).show();
        RecyclerView.LayoutManager ll = new LinearLayoutManager(MyApplication.getContext());
        rv_contacts.setLayoutManager(ll);

        ContactsAdapter contactsAdapter = new ContactsAdapter(contactList);
        rv_contacts.setAdapter(contactsAdapter);
    }
}
